//
//  WmcEpidemicTemplateView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/7.
//

import UIKit

/// 疫情水印
class WmcEpidemicTemplateView: WmcBaseWatermarkTemplateView {
    /// 渐变色视图
    lazy var titleBgView: WmcBaseGradientView = {
        let view = WmcBaseGradientView()
        if let layer = view.layer as? CAGradientLayer {
            layer.startPoint = CGPoint(x: 0, y: 0)
            layer.endPoint = CGPoint(x: 1, y: 0)
            layer.colors = [UIColor.wmc_blue_51CEFE.yp.cgColor, UIColor.wmc_blue_5290FD.yp.cgColor]
        }
        view.layer.cornerRadius = 8
        return view
    }()

    lazy var titleIconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()

    override func config() {
        super.config()
        defaultWidth = 225.wmc_autoHor

        addSubview(titleBgView)

        addSubview(titleIconImageView)

        titleLabel.font = 16.yp.uiFont(weight: .bold)
        titleLabel.backgroundColor = .clear
        titleLabel.textAlignment = .center
        addSubview(titleLabel)

        stackView.axis = .vertical
        stackView.distribution = .equalSpacing
        stackView.alignment = .fill
        stackView.spacing = 0
        addSubview(stackView)
    }

    override func configWatermarkModel(_ watermarkModel: WmcWatermarkDetailModel, styleMode: WmcBaseWatermarkTemplateView.StyleMode) {
        super.configWatermarkModel(watermarkModel, styleMode: styleMode)

        let alpha = calculateAlpha(watermarkModel.alpha)
        titleIconImageView.alpha = alpha
        titleBgView.alpha = alpha

        titleIconImageView.kf.setImage(with: watermarkModel.icon.yp.url, placeholder: "wm_epidemic_title_icon".wmc_image)

        var drawViews: [UIView] = []
        // 获取标题最大字数
        let maxTitleCount = watermarkModel.fieldTitleMaxCount
        watermarkModel.fields.forEach { option in
            guard option.isOpen else { return }
            switch option.fieldType {
            case .title: // 标题
                titleLabel.text = option.content
                titleLabel.textColor = option.uiTextColor

            case .time: // 时间
                let item = createDateItem(option: option,
                                          styleMode: styleMode,
                                          staticDate: watermarkModel.staticData.staticDate,
                                          titleStyle: .noTitle,
                                          maxTitleCount: maxTitleCount,
                                          dateFormat: .yyyy_MM_dd_week_HH_mm)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .addr: // 地点
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .location)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .lat: // 经纬
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .longitudLatitude)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .weather: // 天气
                let item = createWeatherItem(option: option,
                                             styleMode: styleMode,
                                             staticWeater: watermarkModel.staticData.staticWeater,
                                             maxTitleCount: maxTitleCount,
                                             weatherFormat: .weatherTemperature)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .altitude: // 海拔
                let item = createAltitudeItem(option: option,
                                              styleMode: styleMode,
                                              staticLocation: watermarkModel.staticData.staticLocation,
                                              maxTitleCount: maxTitleCount,
                                              altitudeFormat: .meters)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .azimuth: // 方位角
                let item = createAzimuthItem(option: option,
                                             styleMode: styleMode,
                                             staticAzimuth: watermarkModel.staticData.staticAzimuth,
                                             maxTitleCount: maxTitleCount,
                                             azimuthFormat: .default)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .decibe: // 分贝
                let item = createDecibeItem(option: option,
                                            styleMode: styleMode,
                                            maxTitleCount: maxTitleCount,
                                            decibeFormat: .describeDB)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .speed: // 速度
                let item = createSpeedItem(option: option,
                                           styleMode: styleMode,
                                           staticLocation: watermarkModel.staticData.staticLocation,
                                           maxTitleCount: maxTitleCount,
                                           speedFormat: .km_h)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .remark,
                 .record,
                 .text,
                 .custom: // 备注、记工、输入
                let item = createDefaultTextItem(option: option,
                                                 maxTitleCount: maxTitleCount)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .brand: // 品牌图
                configOptionBrand(option)

            default: break
            }
        }

        drawViews.forEach { view in
            setShadow(view)
        }

        // 添加进堆栈视图
        stackView.yp.arrangedSubviews = drawViews

        makeConstraints()
    }

    override func makeConstraints() {
        guard let watermarkModel = watermarkModel else { return }
        // 标题可隐藏
        var isHiddenTitle = true
        watermarkModel.fields.forEach { option in
            if option.fieldType == .title, option.isOpen {
                isHiddenTitle = false
                return
            }
        }

        titleIconImageView.isHidden = isHiddenTitle
        titleBgView.isHidden = isHiddenTitle
        titleLabel.isHidden = isHiddenTitle

        if !isHiddenTitle { // 不隐藏
            // 布局
            titleIconImageView.snp.remakeConstraints { make in
                make.top.left.equalTo(8)
                make.size.equalTo(32)
            }

            titleBgView.snp.remakeConstraints { make in
                make.top.bottom.equalTo(titleIconImageView)
                make.right.equalTo(-8)
                make.left.equalTo(titleIconImageView.snp.centerX).offset(-titleBgView.layer.cornerRadius)
            }

            titleLabel.snp.remakeConstraints { make in
                make.top.bottom.right.equalTo(titleBgView)
                make.left.equalTo(titleIconImageView.snp.right)
            }
        }

        super.makeConstraints()

        stackView.snp.remakeConstraints { make in
            if !isHiddenTitle { // 不隐藏
                make.top.equalTo(titleBgView.snp.bottom).offset(8)
            } else {
                make.top.equalTo(8)
            }
            make.left.equalToSuperview()
            make.right.equalTo(-8)
            if isShowCertificationWatermark {
                make.bottom.equalTo(certificationView.snp.top).offset(-4)
            } else {
                make.bottom.equalToSuperview().offset(-8)
            }
        }
    }
}
